<template>
  <div class="app-container">

    <el-row :gutter="10" class="mb8">
      <el-col :span="12" class="card-box">
      <el-card>
        <div slot="header"><span><i class="el-icon-cpu"></i>单体类型信息</span></div>
        <div class="el-table el-table--enable-row-hover el-table--medium">
          <table style="width: 100%;">
            <thead>
            <tr>
              <th class="el-table__cell is-leaf">
                <div class="cell">属性</div>
              </th>
              <th class="el-table__cell is-leaf">
                <div class="cell">值</div>
              </th>
            </tr>
            </thead>
            <tbody>
            <tr>
              <td class="el-table__cell is-leaf">
                <div class="cell">单体类型</div>
              </td>
              <td class="el-table__cell is-leaf">
                <div class="cell">{{ this.cellTypeName }}</div>
              </td>
            </tr>
            <tr>
              <td class="el-table__cell is-leaf">
                <div class="cell">单体最大数量</div>
              </td>
              <td class="el-table__cell is-leaf">
                <div class="cell">{{ this.cellTypeNum }}</div>
              </td>
            </tr>
            </tbody>
          </table>
        </div>
      </el-card>
      </el-col>
    </el-row>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          @click="handleUpdate"
        >编辑
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="updated"
          @click="handleDelete"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-close"
          size="mini"
          @click="handleClose"
        >关闭
        </el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="dataList">
      <el-table-column label="开始编号" align="center" prop="startNo"/>
      <el-table-column label="结束编号" align="center" prop="endNo"/>
      <el-table-column label="映射数量" align="center" prop="reflectionNum"/>
      <el-table-column label="映射类型" align="center" prop="reflectionTypeName" width="280" :show-overflow-tooltip="true"/>
    </el-table>

    <!-- 添加或修改参数配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="668px" append-to-body>
      <el-button type="primary" class="add-btn" @click="addLine">添加行</el-button>
      <el-table :data="tableData" size="mini"
                stripe
                highlight-current-row
                border
                style="width: 97.3%"
                class="el-tb-edit"
                :header-cell-style="{background:'#2a87ed',color:'#fff',fontSize: '1.0rem',fontWeight: 'normal',height: '2.58rem'}"
                ref="dataList"
      >
        <el-table-column prop="startNo" label="开始编号" align="center" width="100">
          <template slot-scope="scope">
            <el-input v-model="scope.row.startNo" type="number" min="1" max="300"/>
          </template>
        </el-table-column>
        <el-table-column prop="endNo" label="结束编号" align="center" width="100">
          <template slot-scope="scope">
            <el-input v-model="scope.row.endNo" type="number" min="1" max="300"/>
          </template>
        </el-table-column>
        <el-table-column prop="reflectionNum" label="映射数量" align="center" width="100">
          <template slot-scope="scope">
            <el-input v-model="scope.row.reflectionNum" type="number" min="1" max="300"/>
          </template>
        </el-table-column>
        <el-table-column prop="reflectionType" label="映射类型" align="center" width="220">
          <template slot-scope="scope">
            <el-select
              v-model="scope.row.reflectionType"
              placeholder="映射类型"
              clearable
              style="width: 190px">
              <el-option
                v-for="dict in dict.type.cell_reflection_type"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"/>
            </el-select>
          </template>
        </el-table-column>
        <el-table-column prop="accident" label="操作" align="center" width="100">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDeleteDataDictionary(scope.$index,tableData)"
            >删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getCellReflectionList,
  addCellReflection,
  updateCellReflection,
  delCellReflection
}
  from "@/api/system/station/cell_reflection";
import {delCellType, getCellTypeDetail}
  from "@/api/system/station/cell_type";

export default {
  name: "Data",
  dicts: ['cell_type_list', 'cell_reflection_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      updated: true,
      addFlag: true,
      //数据字典资产信息的集合
      dataId: 1,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 字典表格数据
      dataList: [],
      tableData: [],
      stationId: '',
      cellTypeId: undefined,
      cellTypeName: '',
      cellTypeNum: '',
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {
        cellTypeId: undefined,
        list: []
      }
    };
  },
  created() {
    this.cellTypeId = this.$route.params.cellTypeId;
    this.getCellTypeDetail(this.cellTypeId);
    this.getList(this.cellTypeId);
  },
  methods: {
    /** 删除按钮操作 */
    handleDeleteDataDictionary(index, rows) {
      this.$modal.confirm('是否删除当前行？').then(function () {
        rows.splice(index, 1); //对tableData中的数据删除一行
      }).then(() => {
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    addLine() {
      let d = {
        index: this.dataId++,
        startNo: "", //资产编号实时回显
        endNo: "",
        reflectionNum: "",
        reflectionType: ""
      };
      this.tableData.push(d);
    },
    /** 删除按钮操作 */
    handleDelete() {
      this.$modal.confirm('是否确认删除单体类型为"' + this.cellTypeName + '"的数据项？')
        .then(() => {
          return delCellReflection(this.cellTypeId);
        }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });

    },
    getCellTypeDetail() {
      this.loading = true;
      getCellTypeDetail(this.cellTypeId).then(response => {
        // this.dataList = response.data;
        this.cellTypeName = response.data.cellTypeName;
        this.cellTypeNum = response.data.cellTypeNum;
        this.loading = false;
      });
    },
    /** 查询数据列表 */
    getList() {
      this.loading = true;
      getCellReflectionList(this.cellTypeId).then(response => {
        this.dataList = response.data;
        this.updated = !this.dataList.length
        this.addFlag = !this.dataList.length
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        cellTypeId: undefined,
        list: []
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 返回按钮操作 */
    handleClose() {
      const obj = {path: "/system/station"};
      this.$tab.closeOpenPage(obj);
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加单体映射类型数据";
    },
    /** 修改按钮操作 */
    handleUpdate() {
      this.reset();
      this.open = true;
      this.title = "修改单体类型数据";
      getCellReflectionList(this.cellTypeId).then(response => {
        this.tableData = response.data;
        this.open = true;
        this.title = "添加单体映射类型数据";
        this.addFlag = false;
      });
    },

    /** 提交按钮 */
    submitForm: function () {
      this.form.cellTypeId = this.cellTypeId;
      this.form.list = this.tableData;
      if (this.addFlag) {
        addCellReflection(this.form).then(response => {
          this.$modal.msgSuccess("新增成功");
          this.open = false;
          this.getList();
        });
      } else {
        updateCellReflection(this.form).then(response => {
          this.$modal.msgSuccess("修改成功");
          this.open = false;
          this.getList();
        });
      }
    }

  }
};
</script>
